package com.bajiao.app.controller.user;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.constant.CommonConstant;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.menu.ResultEnum;
import com.bajiao.common.web.entity.TableDataInfo;
import com.bajiao.entity.industry.Industry;
import com.bajiao.entity.store.StoreUser;
import com.bajiao.entity.user.User;
import com.bajiao.service.store.StoreUserService;
import com.bajiao.service.user.UserCouponService;
import com.bajiao.service.user.UserService;
import com.bajiao.third.utils.AliSmsUtils;
import com.bajiao.vo.coupon.CouponResponse;
import com.bajiao.vo.user.InviteRecord;
import com.bajiao.vo.user.UserDetails;
import com.bajiao.vo.user.UserInfo;
import com.bajiao.vo.user.UserProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.List;

/**
 * @description: 普通用户
 * @author: FanJq
 * @date: 2024-10-10
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/v1/user")
public class RestUserController extends AppBaseController {

    @Resource
    private UserService userService;
    @Resource
    private StoreUserService storeUserService;

    @PostMapping("/setInterest")
    @ApiOperation(value = "设置兴趣")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "interest", value = "兴趣(字符串 多个,分割)", required = true, dataType = "String")
    })
    public RestResult setInterest(@ModelAttribute("userId") String userId,
                                  @RequestParam("interest") String interest) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        userService.setInterest(userId, interest);
        return success();
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "用户信息")
    public RestResult<UserInfo> getUserInfo(@ModelAttribute("user") User user) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        UserInfo response = userService.getUserInfo(user);
        return success(response);
    }

    @GetMapping(value = "/modify")
    @ApiOperation(value = "修改用户资料")
    public RestResult<UserInfo> modify(@ModelAttribute("userId") String userId,
                                       UserInfo request) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        UserInfo response = userService.modify(userId, request);
        return success(response);
    }

    @GetMapping(value = "/property")
    @ApiOperation(value = "用户资产")
    public RestResult<UserProperty> getUserProperty(@ModelAttribute("userId") String userId) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        UserProperty response = userService.getProperty(userId);
        return success(response);
    }

    @GetMapping(value = "/invite/code")
    @ApiOperation(value = "获取邀请码")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "source", value = "applet-小程序 ios-ios android-安卓", required = true, dataType = "String")
    })
    public RestResult<String> getInviteCode(@ModelAttribute("user") User user,
                                            @RequestParam("source") String source) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        String qrCode = userService.getInviteCode(user.getInviteCode(), source);
        return success("", qrCode);
    }

    @GetMapping(value = "/getInviteCode")
    @ApiOperation(value = "获取邀请码名文")
    public RestResult<String> getUserInviteCode(@ModelAttribute("user") User user) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        return success("ok", "https://mtzfront.hzgqmxt.com");
    }

    @GetMapping(value = "/invite/record")
    @ApiOperation(value = "推广记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "Integer", required = true)
    })
    public RestResult<TableDataInfo<InviteRecord>> getInviteRecord(@ModelAttribute("userId") String userId) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        startPage();
        List<InviteRecord> resultList = userService.getInviteRecord(userId);
        return success(getDataTable(resultList));
    }

    @GetMapping(value = "/coupon/page")
    @ApiOperation(value = "我的优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", dataType = "Integer", required = true)
    })
    public RestResult<TableDataInfo<CouponResponse>> getUserCouponPage(@ModelAttribute("userId") String userId) {
        if (StrUtil.isBlank(userId))
            return tokenOverdue();
        startPage();
        List<CouponResponse> resultList = userService.getUserCoupon(userId);
        return success(getDataTable(resultList));
    }

    @PostMapping(value = "/forget/cipher")
    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String", required = true),
            @ApiImplicitParam(name = "captcha", value = "验证码", dataType = "String", required = true),
            @ApiImplicitParam(name = "cipher", value = "密码", dataType = "String", required = true)
    })
    public RestResult<UserDetails> forgetCipher(@RequestParam String mobile,
                                                @RequestParam String captcha,
                                                @RequestParam String cipher) {
        //@create by FanJq 2024/12/25 17:18 校验验证码
        AliSmsUtils.check(mobile, CommonConstant.SMS_EVENT_FORGET, captcha);

        //@create by FanJq 2024/12/25 17:05 获取用户信息
        User user = userService.queryByMobileAndNotDelFlag(mobile);
        //@create by FanJq 2024/7/8 15:23 校验是否注册
        if (ObjectUtil.isNull(user))
            return fail(ResultEnum.NO_BIND);
        userService.modifyCipher(user, cipher);
        return success();
    }

    @PostMapping(value = "/modify/cipher")
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "captcha", value = "验证码", dataType = "String", required = true),
            @ApiImplicitParam(name = "cipher", value = "密码", dataType = "String", required = true),
            @ApiImplicitParam(name = "source", value = "ios android", dataType = "String", required = true)
    })
    public RestResult<UserDetails> forgetCipher(@ModelAttribute("user") User user,
                                                @RequestParam String captcha,
                                                @RequestParam String cipher) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        //@create by FanJq 2024/12/25 17:18 校验验证码
        AliSmsUtils.check(user.getMobile(), CommonConstant.SMS_EVENT_CHANGE, captcha);
        userService.modifyCipher(user, cipher);
        return success();
    }

    @GetMapping(value = "/identity")
    @ApiOperation(value = "获取用户身份", notes = "0-普通用户 1-店铺管理员 2-店铺成员")
    public RestResult getUserIdentity(@ModelAttribute("user") User user) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        StoreUser storeUser = storeUserService.getByAccount(user.getMobile());
        Integer result = ObjectUtil.isNotNull(storeUser) ? storeUser.getType() : 0;
        return success("", result);
    }
}
